﻿/// <summary>
/// <Filename>: Motion_Camera.cs
/// Author: Jiang Xiaolong
/// Created: 2015.01.29
/// Version: 1.0
/// Company: Sunnytech
/// Function: Camera运动信息处理类
///
/// Changed By:
/// Modification Time:
/// Discription:
/// <summary>
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using Motion.MotionPlatform.Tools;
using System.Reflection;

namespace Motion.MotionPlatform
{
	//摄像机运动类
	public class CameraMotion : IMotion
	{
		//默认为Active
		public CurrentState State
		{
			get { return _state; }
			set { _state = value; }
		}
		private CurrentState _state = CurrentState.Active;
		//时间参数
		//正常速率时间
		public float StandardTime
		{
			get { return _standardTime; }
			set { _standardTime = value; }
		}
		private float _standardTime = 0f;
		//运动开始时间
		public float StartTime
		{
			get { return _startTime; }
			set { _startTime = value; }
		}
		private float _startTime = 0f;
		//运动结束时间
		public float EndTime
		{
			get { return _endTime; }
			set { _endTime = value; }
		}
		private float _endTime = 0f;

		//静态类属性
		//当前活动相机
		public static Camera CurrentCamera = null;
		//之前的相机
		public static Camera FormerCamera = null;
		//当前字符串
		public static string CurrentStr = "Main Camera";
		//当前类型,True为透视，False为正交
		public static bool CurrentType = false;
		//历史摄像机字符串，从Excel表格中获取
		public static List<string> CameraString = new List<string>();
		//历史摄像机，从Excel表格中获取
		public static List<Camera> CameraList = new List<Camera>();
		//历史摄像机类型，从Excel表格获取摄像机后自动判断,True为透视，False为正交
		public static List<bool> CameraType = new List<bool>();
		//是否要切换摄像机
		public static bool NeedChange = false;
		//观察物体
		public static Transform TargetTrans = null;

		//运动属性
		public CameraMotionType CurrentMotion = CameraMotionType.Line;

		//速率
		public float SpeedRate
		{
			get { return Motion_Data.SpeedRate; }
			set { Motion_Data.SpeedRate = value; }
		}
		//暂停状态
		public bool PauseControl
		{
			get { return Motion_Data.PauseControl; }
			set { Motion_Data.PauseControl = value; }
		}

		//开始位置
		public Vector3 StartPos = new Vector3(0f, 0f, 0f);
		//结束位置
		public Vector3 EndPos = new Vector3(0f, 0f, 0f);
		//速度矢量
		public Vector3 SpeedVector = new Vector3(0f, 0f, 0f);
		public float RotateSpeed = 1.0f;
		//旋转中心
		public Vector3 RotateCenter = new Vector3(0f, 0f, 0f);
		public Vector3 RotateAxis = new Vector3(0f, 0f, 0f);

		//视域缩放参数
		public bool HasSizeChange = false;
		public float StartSize = 0f;
		public float EndSize = 0f;
		public float SizeSpeed = 0f;

		public bool needExcess = false;  //是否需要过渡
		public bool needSetPos = false;  //是否需要设置初始位置
		public CRSpline crSpline = new CRSpline();  //CR样条
		public Vector3[] diffVector = new Vector3[] { Vector3.zero, Vector3.zero };  //角度过度
		private int currentIndex = 0;  //当前二分法找到的参数序号
		private float viewTimeRate = 0f;  //二分法找到的参数对应的时间
		private float currentTimeRate = 0f;  //记录当前时间进程
		public float startTimeRate = 0f;  //记录变速时的当前时间进程
		private float restTime = 0f;  //剩余时间
		private float postTime = 0f;  //已经过去的时间
		private float lastFrameTime = 0f;  //记录上一帧时间

		//结构初始化
		public void Init()
		{
			restTime = EndTime;

			if (NeedChange)
			{
				if (FormerCamera != null)
				{
					FormerCamera.enabled = false;
				}
				CurrentCamera.enabled = true;
			}
			if (needSetPos)
			{
				CurrentCamera.transform.position = StartPos;
			}
			if (CurrentMotion == CameraMotionType.VisualPath)
			{
				if (Motion_Data.isEditor)  //显示绿线初始化
				{
					Type _t = Motion_TypeGet.MotionTypeGet("ReflectionManager");
					if (!MotionPlatformData.st_ReflectionManager && _t != null)
					{
						MotionPlatformData.st_ReflectionManager = (MonoBehaviour)GameObject.FindObjectOfType(_t);
					}

					if (MotionPlatformData.st_ReflectionManager && _t != null)
					{
						MethodInfo methodInfo = _t.GetMethod("AddUnit");
						BindingFlags flag = BindingFlags.Public | BindingFlags.Instance;
						object[] para = new object[] { CurrentCamera.transform, crSpline.controlPoints.ToArray() };
						methodInfo.Invoke(MotionPlatformData.st_ReflectionManager, flag, Type.DefaultBinder, para, null);
					}
				}
			}
		}

		//暂停控制
		public void Pause(bool play_status)
		{

		}

		//速率控制
		public void ChangeRate(float current_rate, float current_time)
		{
			if (State != CurrentState.Active)
			{
				StartTime = 0;
				EndTime = SpeedRate * EndTime / current_rate;
			}
			else
			{
				//激活状态
				startTimeRate = currentTimeRate;
				postTime = current_time;
				EndTime = current_time + (EndTime - current_time) * SpeedRate / current_rate;
				restTime = (EndTime - postTime) / (1f - startTimeRate);
				if (CurrentMotion == CameraMotionType.Line)
				{
					StartPos = CurrentCamera.transform.position;
					//CurrentCamera.transform.LookAt(TargetTrans);
				}
				if (HasSizeChange)
				{
					StartSize = GetCameraPortValue();
				}
				StartTime = current_time;
			}
		}

		//运动
		public void Move(float current_time, float speed_rate, float delta_time)
		{
			if (TimeRate(current_time) > 1.001f || current_time > EndTime)
			{
				return;
			}
			else
			{
				lastFrameTime = current_time;
			}
			if (CurrentMotion == CameraMotionType.Line)
			{  //直线
				CurrentCamera.transform.position = StartPos + SpeedVector * (current_time - StartTime) * speed_rate;
				if (HasSizeChange)
					SetCameraPortValue(StartSize + SizeSpeed * (current_time - StartTime) * speed_rate);
				if (needExcess)
				{
					if (TimeRate(current_time) <= 0.1f)
					{
						CurrentCamera.transform.eulerAngles = Vector3.Lerp(diffVector[0], diffVector[1], TimeRate(current_time) * 10f);
					}
					else
					{
						CurrentCamera.transform.LookAt(TargetTrans);
					}
				}
				else
				{
					CurrentCamera.transform.LookAt(TargetTrans);
				}
			}
			else if (CurrentMotion == CameraMotionType.Circular)  //圆弧
			{
				CurrentCamera.transform.RotateAround(RotateCenter, RotateAxis, RotateSpeed * delta_time * speed_rate);
				if (HasSizeChange)
					SetCameraPortValue(StartSize + SizeSpeed * (current_time - StartTime) * speed_rate);
				CurrentCamera.transform.LookAt(TargetTrans);
			}
			else  //可视化路径运动
			{
				CurrentCamera.transform.position = crSpline.Interp(TimeRate(current_time));
				if (TargetTrans != null)
				{
					if (needExcess)
					{
						if (TimeRate(current_time) <= 0.1f)
						{
							CurrentCamera.transform.eulerAngles = Vector3.Lerp(diffVector[0], diffVector[1], TimeRate(current_time) * 10f);
						}
						else
						{
							CurrentCamera.transform.LookAt(TargetTrans);
						}
					}
					else
					{
						CurrentCamera.transform.LookAt(TargetTrans);
					}
					SetCameraPortValue(crSpline.ViewLerp(TimeRate(current_time)));
				}
				else
				{
					CurrentCamera.transform.eulerAngles = crSpline.EulerAngleLerp(TimeRate(current_time), ref currentIndex, ref viewTimeRate);
					//二分法查找算一次
					SetCameraPortValue(Mathf.Lerp(crSpline.cameraViewList[currentIndex], crSpline.cameraViewList[currentIndex + 1], viewTimeRate));
				}
			}
		}

		//变速后的时间比率
		private float TimeRate(float current_time)
		{
			currentTimeRate = startTimeRate + (current_time - postTime) / restTime;
			return currentTimeRate;
		}

		//时间管理
		public bool TimesUp(float current_time)
		{
			if (current_time > EndTime)
			{
				if (EndTime - lastFrameTime > 0)  //最后没运行的时间修正
				{
					TimeCorrection(EndTime - lastFrameTime, SpeedRate);
				}
				return true;
			}
			else
				return false;
		}

		//后处理，主要是位置修正
		public void PostProcess()
		{
			if (CurrentMotion == CameraMotionType.Line || CurrentMotion == CameraMotionType.Circular)
			{
				CurrentCamera.transform.position = EndPos;
				CurrentCamera.transform.LookAt(TargetTrans);
				if (HasSizeChange)
					SetCameraPortValue(EndSize);
			}
			else
			{
				if (Motion_Data.isEditor)  //消除绿线初始化
				{
					Type _t = Motion_TypeGet.MotionTypeGet("ReflectionManager");
					if (!MotionPlatformData.st_ReflectionManager && _t != null)
					{
						MotionPlatformData.st_ReflectionManager = (MonoBehaviour)GameObject.FindObjectOfType(_t);
					}

					if (MotionPlatformData.st_ReflectionManager && _t != null)
					{
						MethodInfo methodInfo = _t.GetMethod("DestroyUnit");
						BindingFlags flag = BindingFlags.Public | BindingFlags.Instance;
						object[] para = new object[] { CurrentCamera.transform };
						methodInfo.Invoke(MotionPlatformData.st_ReflectionManager, flag, Type.DefaultBinder, para, null);
					}
				}
				CurrentCamera.transform.position = crSpline.controlPoints[crSpline.controlPoints.Count - 1];
				SetCameraPortValue(crSpline.cameraViewList[crSpline.cameraViewList.Count - 1]);
				if (TargetTrans != null)
				{
					CurrentCamera.transform.LookAt(TargetTrans);
				}
				else
				{
					CurrentCamera.transform.eulerAngles = crSpline.rotationList[crSpline.rotationList.Count - 1];
				}
			}
		}

		//最后没运行的时间修正
		private void TimeCorrection(float delta_time, float speed_rate)
		{
			if (CurrentMotion == CameraMotionType.Line)
			{  //直线
				CurrentCamera.transform.position = StartPos + SpeedVector * (EndTime - StartTime) * speed_rate;
				if (HasSizeChange)
					SetCameraPortValue(StartSize + SizeSpeed * (EndTime - StartTime) * speed_rate);
				if (needExcess)
				{
					if (TimeRate(EndTime) <= 0.1f)
					{
						CurrentCamera.transform.eulerAngles = Vector3.Lerp(diffVector[0], diffVector[1], TimeRate(EndTime) * 10f);
					}
					else
					{
						CurrentCamera.transform.LookAt(TargetTrans);
					}
				}
				else
				{
					CurrentCamera.transform.LookAt(TargetTrans);
				}
			}
			else if (CurrentMotion == CameraMotionType.Circular)  //圆弧
			{
				CurrentCamera.transform.RotateAround(RotateCenter, RotateAxis, RotateSpeed * delta_time * speed_rate);
				if (HasSizeChange)
					SetCameraPortValue(StartSize + SizeSpeed * (EndTime - StartTime) * speed_rate);
				CurrentCamera.transform.LookAt(TargetTrans);
			}
			else  //可视化路径运动
			{
				CurrentCamera.transform.position = crSpline.Interp(TimeRate(EndTime));
				if (TargetTrans != null)
				{
					if (needExcess)
					{
						if (TimeRate(EndTime) <= 0.1f)
						{
							CurrentCamera.transform.eulerAngles = Vector3.Lerp(diffVector[0], diffVector[1], TimeRate(EndTime) * 10f);
						}
						else
						{
							CurrentCamera.transform.LookAt(TargetTrans);
						}
					}
					else
					{
						CurrentCamera.transform.LookAt(TargetTrans);
					}
					SetCameraPortValue(crSpline.ViewLerp(TimeRate(EndTime)));
				}
				else
				{
					CurrentCamera.transform.eulerAngles = crSpline.EulerAngleLerp(TimeRate(EndTime), ref currentIndex, ref viewTimeRate);
					//二分法查找算一次
					SetCameraPortValue(Mathf.Lerp(crSpline.cameraViewList[currentIndex], crSpline.cameraViewList[currentIndex + 1], viewTimeRate));
				}
			}
		}

		//摄像机视域信息获取
		public float GetCameraPortValue()
		{
			if (CurrentType)
				return CurrentCamera.fieldOfView;
			else
				return CurrentCamera.orthographicSize;
		}

		//摄像机视域信息设置
		private void SetCameraPortValue(float value)
		{
			if (CurrentType)
				CurrentCamera.fieldOfView = value;
			else
				CurrentCamera.orthographicSize = value;
		}

		//直线上的点
		public Vector3 LineInterp(float rate)
		{
			return Vector3.Lerp(StartPos, EndPos, rate);
		}
	}

	/// <summary>
	/// Camera运动信息提取管理类，相当于工厂；
	/// </summary>
	public class Motion_CameraInfoManager : IMotionInfoManager
	{
		/// <summary>
		/// 信息获取函数，用反射的方式创建Camera相应的解析类；
		/// </summary>
		/// <param name="motion_symbol">当前运动标识符；</param>
		/// <param name="data_row">当前传入的Excel数据行；</param>
		/// <param name="row_id">当前行对应ID号；</param>
		/// <param name="group_table">当前Group数据表；</param>
		/// <param name="motion_list">简单运动类列表；</param>
		/// <param name="complex_motion_list">复合运动类列表；</param>
		/// <returns>解析是否正确；</returns>
		public bool InfoGet(string motion_symbol, int state, DataRow data_row, string row_id, DataTable group_table, IList<IMotion> motion_list, IList<IList<IMotion>> complex_motion_list)
		{
			string motionTypeID = "ONLY";
			if (Motion_Data.MotionFunctionAllow(motion_symbol, motionTypeID, state))
			{
				if (Motion_Data.MotionFunctionClassDic[motion_symbol].ContainsKey(motionTypeID))
				{
					Type _t = Motion_TypeGet.MotionTypeGet("Motion.MotionPlatform." + Motion_Data.MotionFunctionClassDic[motion_symbol][motionTypeID]);
					//实例化相应的解析类
					IMotionFunctionInfo infoInstance = (IMotionFunctionInfo)Activator.CreateInstance(_t);

					////将Excel中参数转换成参数表里面的数据
					//Motion_Compute.ExcelDataAnalysis(ref data_row);
					return infoInstance.InfoGet(data_row, row_id, group_table, motion_list, complex_motion_list);
				}
				else
				{
					Debug.LogError("Camera运动标志符：" + motionTypeID + "，不含有该运动标志符对应的解析类，请询问相关程序猿！");
					return false;
				}
			}
			else
			{
				return true;
			}
		}
	}


	/// <summary>
	/// 相机运动；
	/// </summary>
	public class Motion_Camera_Only : IMotionFunctionInfo
	{

		public bool InfoGet(DataRow data_row, string row_id, DataTable group_table, IList<IMotion> motion_list, IList<IList<IMotion>> complex_motion_list)
		{
			CameraMotion _cameraMotion = new CameraMotion();

			//相机状态记录参数
			Hashtable cameraHashTable = new Hashtable();
			Hashtable cameraHashTable2 = new Hashtable();
			Hashtable cameraHashTable3 = new Hashtable();
			bool needRecordState = false;
			CameraMotion.TargetTrans = null;
			//摄像机名称检查
			string cameraName = (string)data_row[2].ToString();
			bool is_right = Check(cameraName);
			if (!is_right)
			{
				//摄像机名称出错
				if (Motion_Data.isEditor)
				{
					Debug.LogError(ErrorLocation.Locate("CAMERA", "NAME", row_id) + "，摄像机名称错误！");
				}
				return false;
			}
			cameraHashTable.Add("Camera", CameraMotion.CurrentCamera);

			if (CameraMotion.NeedChange)
			{
				cameraHashTable2.Add("Camera", CameraMotion.FormerCamera);
				cameraHashTable2.Add("Enable", false);

				cameraHashTable3.Add("Camera", CameraMotion.CurrentCamera);
				cameraHashTable3.Add("Enable", true);
			}

			//圆弧运动、直线和路径运动确定，圆弧优先级高于其他，根据【旋转参考物体:9】是否有填来确定
			string rotateRefStr = (string)data_row[9].ToString();
			string pathName = "";
			if (rotateRefStr == "")
			{
				pathName = (string)data_row[14].ToString();
				if (pathName == "")
					_cameraMotion.CurrentMotion = CameraMotionType.Line;  //直线运动
				else
					_cameraMotion.CurrentMotion = CameraMotionType.VisualPath;  //可视化路径运动
			}
			else
			{  //圆弧运动
				_cameraMotion.CurrentMotion = CameraMotionType.Circular;
				try
				{
					_cameraMotion.RotateCenter = GameObject.Find(rotateRefStr).transform.position;
				}
				catch
				{
					is_right = false;
					if (Motion_Data.isEditor)
					{
						Debug.LogError(ErrorLocation.Locate("CAMERA", "ROTATEREF", row_id) + "，旋转参考物体填写错误！");
					}
					return false;
				}
			}
			//观察中心物体处理
			string targetTransStr = (string)data_row[3].ToString();
			if (targetTransStr != "")
			{
				try
				{
					CameraMotion.TargetTrans = GameObject.Find(targetTransStr).transform;
				}
				catch
				{
					is_right = false;
					//摄像机名称出错
					if (Motion_Data.isEditor)
					{
						Debug.LogError(ErrorLocation.Locate("CAMERA", "OBSERVER", row_id) + "，观察物体填写错误！");
					}
					return false;
				}
			}
			else
			{
				if (_cameraMotion.CurrentMotion != CameraMotionType.VisualPath)
				{
					is_right = false;
					//摄像机名称出错
					if (Motion_Data.isEditor)
					{
						Debug.LogError(ErrorLocation.Locate("CAMERA", "OBSERVER", row_id) + "，直线和圆弧模式下必须有观察物体！");
					}
					return false;
				}
			}
			//起始位置确定
			bool isRelativePos = false;  //用于目标位置确定
			string relativeRefStr = (string)data_row[4].ToString();
			string startPosStr = (string)data_row[5].ToString();
			string endPosStr = (string)data_row[6].ToString();
			GameObject empty = new GameObject();
			empty.name = "camera_motion_empty-" + row_id;
			Transform relativeTrans = null;
			if (startPosStr == "")
			{
				_cameraMotion.StartPos = CameraMotion.CurrentCamera.transform.position;
				_cameraMotion.needSetPos = false;
			}
			else
			{
				//默认为世界坐标位置
				_cameraMotion.StartPos = Motion_Compute.Vector3Conversion(startPosStr, ref is_right);
				if (!is_right)
				{
					if (Motion_Data.isEditor)
					{
						Debug.LogError(ErrorLocation.Locate("CAMERA", "SPOSITION", row_id) + "，摄像机初始位置错误！");
					}
					return false;
				}
				if (relativeRefStr != "")
				{  //相对坐标位置
					isRelativePos = true;
					try
					{
						relativeTrans = GameObject.Find(relativeRefStr).transform;
					}
					catch
					{
						is_right = false;
						if (Motion_Data.isEditor)
						{
							Debug.LogError(ErrorLocation.Locate("CAMERA", "RELATIVE", row_id) + "，相对位置参考物体填写错误！");
						}
						return false;
					}
					empty.transform.parent = relativeTrans;
					empty.transform.localPosition = _cameraMotion.StartPos;
					_cameraMotion.StartPos = empty.transform.position;
					empty.transform.parent = null;
				}
				if ((_cameraMotion.StartPos - CameraMotion.CurrentCamera.transform.position).magnitude > 0.01f)
					_cameraMotion.needSetPos = true;
				else
					_cameraMotion.needSetPos = false;
			}

			//视域缩放参数确定
			string endSizeStr = (string)data_row[8].ToString();
			if (endSizeStr == "")
			{
				_cameraMotion.HasSizeChange = false;
			}
			else
			{
				_cameraMotion.HasSizeChange = true;
				_cameraMotion.EndSize = Motion_Compute.FloatConversion(endSizeStr, ref is_right);
				if (!is_right)
				{
					if (Motion_Data.isEditor)
					{
						Debug.LogError(ErrorLocation.Locate("CAMERA", "APORT", row_id) + "，摄像机视域变化终值错误！");
					}
					return false;
				}
				string startSizeStr = (string)data_row[7].ToString();
				if (startSizeStr == "")
				{
					_cameraMotion.StartSize = _cameraMotion.GetCameraPortValue();
				}
				else
				{
					_cameraMotion.StartSize = Motion_Compute.FloatConversion(startSizeStr, ref is_right);
					if (!is_right)
					{
						if (Motion_Data.isEditor)
						{
							Debug.LogError(ErrorLocation.Locate("CAMERA", "SPORT", row_id) + "，摄像机视域变化初值错误！");
						}
						return false;
					}
				}
				needRecordState = true;
				cameraHashTable["View"] = _cameraMotion.EndSize;
			}
			//直线、可视化路径、旋转运动分别处理
			if (_cameraMotion.CurrentMotion == CameraMotionType.Circular)
			{  //圆弧过程
				//旋转速度获取
				_cameraMotion.RotateSpeed = MotionPlatformData.cameraAngleSpeed;
				string customRotStr = (string)data_row[13].ToString();
				if (customRotStr != "")
				{
					_cameraMotion.RotateSpeed = Motion_Compute.FloatConversion(customRotStr, ref is_right);
					if (!is_right)
					{
						if (Motion_Data.isEditor)
						{
							Debug.LogError(ErrorLocation.Locate("CAMERA", "ROTATESPEED", row_id) + "，自定义角速度错误！");
						}
						return false;
					}
				}
				//旋转角度
				float rotateDegree = Motion_Compute.FloatConversion((string)data_row[11].ToString(), ref is_right);
				if (!is_right)
				{
					if (Motion_Data.isEditor)
					{
						Debug.LogError(ErrorLocation.Locate("CAMERA", "DEGREE", row_id) + "，旋转角度错误！");
					}
					return false;
				}
				//旋转轴
				_cameraMotion.RotateAxis = Motion_Compute.Vector3Conversion((string)data_row[10].ToString(), ref is_right);
				if (!is_right)
				{
					if (Motion_Data.isEditor)
					{
						Debug.LogError(ErrorLocation.Locate("CAMERA", "AXIS", row_id) + "，旋转轴错误！");
					}
					return false;
				}
				_cameraMotion.RotateAxis = GameObject.Find(rotateRefStr).transform.TransformDirection(_cameraMotion.RotateAxis);
				//终止信息记录
				GameObject emptyCamera = new GameObject();
				emptyCamera.name = "camera_empty-" + row_id;
				emptyCamera.transform.position = _cameraMotion.StartPos;
				emptyCamera.transform.eulerAngles = CameraMotion.CurrentCamera.transform.eulerAngles;
				emptyCamera.transform.RotateAround(_cameraMotion.RotateCenter, _cameraMotion.RotateAxis, rotateDegree);
				_cameraMotion.EndPos = emptyCamera.transform.position;
				GameObject.DestroyImmediate(emptyCamera);
				//时间
				_cameraMotion.StandardTime = rotateDegree / _cameraMotion.RotateSpeed;
			}
			else
			{
				//直线速度获取
				float moveSpeed = MotionPlatformData.cameraLineSpeed;
				string customMoveStr = (string)data_row[12].ToString();
				if (customMoveStr != "")
				{
					moveSpeed = Motion_Compute.FloatConversion(customMoveStr, ref is_right);
					if (!is_right)
					{
						if (Motion_Data.isEditor)
						{
							Debug.LogError(ErrorLocation.Locate("CAMERA", "MOVESPEED", row_id) + "，自定义直线速度错误！");
						}
						return false;
					}
				}
				if (endPosStr == "" && _cameraMotion.CurrentMotion == CameraMotionType.VisualPath)
				{
					//Do nothing
				}
				else
				{
					//默认为世界坐标位置
					_cameraMotion.EndPos = Motion_Compute.Vector3Conversion(endPosStr, ref is_right);
					if (!is_right)
					{
						if (Motion_Data.isEditor)
						{
							Debug.LogError(ErrorLocation.Locate("CAMERA", "APOSITION", row_id) + "，摄像机目标位置错误，直线时必须填！");
						}
						return false;
					}
					//是相对位置
					if (isRelativePos)
					{
						empty.transform.parent = relativeTrans;
						empty.transform.localPosition = _cameraMotion.EndPos;
						_cameraMotion.EndPos = empty.transform.position;
						empty.transform.parent = null;
					}

				}
				if (_cameraMotion.CurrentMotion == CameraMotionType.VisualPath)  //可视化路径运动
				{
					string isBackString = (string)data_row[10].ToString().ToUpper();
					bool isBack = false;
					if (isBackString == "BACK")
					{
						isBack = true;
					}
					JsonLoad(pathName, _cameraMotion.crSpline, ref is_right, isBack);
					if (!is_right)
					{
						if (Motion_Data.isEditor)
						{
							Debug.LogError(ErrorLocation.Locate("CAMERA", "PATHNAME", row_id) + "，摄像机可视化路径名称填写错误！");
						}
						return false;
					}
					if (startPosStr != "")
						_cameraMotion.crSpline.controlPoints[0] = _cameraMotion.StartPos;
					if (endPosStr != "")
						_cameraMotion.crSpline.controlPoints[_cameraMotion.crSpline.controlPoints.Count - 1] = _cameraMotion.EndPos;
					AngleOptimization(_cameraMotion.crSpline.rotationList);
					_cameraMotion.crSpline.pts = _cameraMotion.crSpline.PathControlPointGenerator(_cameraMotion.crSpline.controlPoints.ToArray());
					_cameraMotion.StandardTime = _cameraMotion.crSpline.TimeGet(_cameraMotion.crSpline.pts, moveSpeed);
					_cameraMotion.crSpline.TimeRateGenerator(_cameraMotion.crSpline.rotationList.Count);
					needRecordState = true;
					if (isBack)
						cameraHashTable["View"] = _cameraMotion.crSpline.cameraViewList[0];
					else
						cameraHashTable["View"] = _cameraMotion.crSpline.cameraViewList[_cameraMotion.crSpline.cameraViewList.Count - 1];
				}
				else  //直线运动过程
				{
					//运动向量
					Vector3 motionVec = _cameraMotion.EndPos - _cameraMotion.StartPos;
					//时间
					_cameraMotion.StandardTime = motionVec.magnitude / moveSpeed;
					//线速度
					_cameraMotion.SpeedVector = motionVec / _cameraMotion.StandardTime;
				}
			}

			if (_cameraMotion.StandardTime == 0)
			{
				is_right = false;
				if (Motion_Data.isEditor)
				{
					Debug.LogError(ErrorLocation.Locate("CAMERA", "ID", row_id) + "，计算出的移动时间为0，请检查信息填写是否正确！");
				}
				return false;
			}
			if (_cameraMotion.HasSizeChange)
			{
				_cameraMotion.SizeSpeed = (_cameraMotion.EndSize - _cameraMotion.StartSize) / _cameraMotion.StandardTime;
			}
			_cameraMotion.EndTime = _cameraMotion.StandardTime / _cameraMotion.SpeedRate;
			//判断摄像机是否需要过渡
			if (CameraMotion.TargetTrans != null && _cameraMotion.CurrentMotion != CameraMotionType.Circular && !_cameraMotion.needSetPos)
			{
				_cameraMotion.diffVector[0] = CameraMotion.CurrentCamera.transform.eulerAngles;
				if (_cameraMotion.CurrentMotion == CameraMotionType.Line)
				{
					empty.transform.position = _cameraMotion.LineInterp(0.1f);
				}
				else
				{
					empty.transform.position = _cameraMotion.crSpline.Interp(0.1f);
				}
				empty.transform.LookAt(CameraMotion.TargetTrans);
				_cameraMotion.diffVector[1] = empty.transform.eulerAngles;
				_cameraMotion.diffVector[1] = new Vector3(AngleClerp(_cameraMotion.diffVector[0].x, _cameraMotion.diffVector[1].x, 1f), AngleClerp(_cameraMotion.diffVector[0].y, _cameraMotion.diffVector[1].y, 1f), AngleClerp(_cameraMotion.diffVector[0].z, _cameraMotion.diffVector[1].z, 1f));
				if ((_cameraMotion.diffVector[1] - _cameraMotion.diffVector[0]).magnitude > 0.1f)
				{
					_cameraMotion.needExcess = true;
				}
				else
				{
					_cameraMotion.needExcess = false;
				}
			}
			else
			{
				_cameraMotion.needExcess = false;
			}
			GameObject.DestroyImmediate(empty);

			Type _t = Motion_TypeGet.MotionTypeGet("ReflectionManager");
			if (!MotionPlatformData.st_ReflectionManager && _t != null)
			{
				MotionPlatformData.st_ReflectionManager = (MonoBehaviour)GameObject.FindObjectOfType(_t);
			}
			//记录相机的视野状态
			if (Motion_Data.isEditor && needRecordState && MotionPlatformData.currentMotion == MotionPlatformState.Teaching)
			{
				if (MotionPlatformData.st_ReflectionManager && _t != null)
				{
					MethodInfo methodInfo = _t.GetMethod("ReflectionAddState");
					BindingFlags flag = BindingFlags.Public | BindingFlags.Instance;
					object[] para = new object[] { MotionPlatformData.excelName, MotionPlatformData.mainRowNumber + 2, "State_CameraView", cameraName, cameraHashTable };
					methodInfo.Invoke(MotionPlatformData.st_ReflectionManager, flag, Type.DefaultBinder, para, null);
				}
			}
			//记录相机是否启用的状态
            //if (Motion_Data.isEditor && (CameraMotion.NeedChange && CameraMotion.FormerCamera != null) && MotionPlatformData.currentMotion == MotionPlatformState.Teaching)
            //{
            //    if (MotionPlatformData.st_ReflectionManager && _t != null)
            //    {
            //        MethodInfo methodInfo = _t.GetMethod("ReflectionAddState");
            //        BindingFlags flag = BindingFlags.Public | BindingFlags.Instance;
            //        object[] para = new object[] { MotionPlatformData.excelName, MotionPlatformData.mainRowNumber + 2, "State_CameraEnable", CameraMotion.FormerCamera.camera.name, cameraHashTable2 };
            //        methodInfo.Invoke(MotionPlatformData.st_ReflectionManager, flag, Type.DefaultBinder, para, null);
            //    }
            //    if (MotionPlatformData.st_ReflectionManager && _t != null)
            //    {
            //        MethodInfo methodInfo = _t.GetMethod("ReflectionAddState");
            //        BindingFlags flag = BindingFlags.Public | BindingFlags.Instance;
            //        object[] para = new object[] { MotionPlatformData.excelName, MotionPlatformData.mainRowNumber + 2, "State_CameraEnable", CameraMotion.CurrentCamera.camera.name, cameraHashTable3 };
            //        methodInfo.Invoke(MotionPlatformData.st_ReflectionManager, flag, Type.DefaultBinder, para, null);
            //    }
            //}

			motion_list.Add(_cameraMotion);

			//自动装配处理
			if (Motion_Data.IsAutoAssemble)
			{
				//用于记录自动装配信息
				string[] autoAssembleContents = new string[data_row.Table.Columns.Count];
				for (int i = 0; i < data_row.Table.Columns.Count; i++)
				{
					autoAssembleContents[i] = data_row[i].ToString();
				}
				//自动装配位置处理
				if (data_row[5].ToString() != "")
				{
					autoAssembleContents[5] = _cameraMotion.EndPos.x + "," + _cameraMotion.EndPos.y + "," + _cameraMotion.EndPos.z;
				}
				autoAssembleContents[6] = _cameraMotion.StartPos.x + "," + _cameraMotion.StartPos.y + "," + _cameraMotion.StartPos.z;
				if (isRelativePos)
				{
					GameObject emptyCamera = new GameObject();
					emptyCamera.name = "camera_empty-" + row_id;
					emptyCamera.transform.position = _cameraMotion.StartPos;
					emptyCamera.transform.parent = relativeTrans;
					autoAssembleContents[6] = emptyCamera.transform.localPosition.x + "," + emptyCamera.transform.localPosition.y + "," + emptyCamera.transform.localPosition.z;
					emptyCamera.transform.position = _cameraMotion.EndPos;
					if (data_row[5].ToString() != "")
					{
						autoAssembleContents[5] = emptyCamera.transform.localPosition.x + "," + emptyCamera.transform.localPosition.y + "," + emptyCamera.transform.localPosition.z;
					}
					emptyCamera.transform.parent = null;
					GameObject.Destroy(emptyCamera);
				}
				//视域值处理
				if (endSizeStr == "")
				{
					autoAssembleContents[7] = "";
					autoAssembleContents[8] = "";
				}
				else
				{
					autoAssembleContents[7] = _cameraMotion.EndSize.ToString();
					autoAssembleContents[8] = _cameraMotion.StartSize.ToString();
				}
				//圆旋转轴处理
				Vector3 autoAxis = -_cameraMotion.RotateAxis;
				autoAssembleContents[10] = autoAxis.x + "," + autoAxis.y + "," + autoAxis.z;
				//可视化BACK处理
				if (_cameraMotion.CurrentMotion == CameraMotionType.VisualPath)
				{
					if (autoAssembleContents[10].ToUpper() == "BACK")
					{
						autoAssembleContents[10] = "";
					}
					else
					{
						autoAssembleContents[10] = "BACK";
					}
				}
				//更新当前行
				Motion_AutoAssemblyAndTime.AutoUpdate(autoAssembleContents, "CAMERA", 1);
			}


			return true;
		}

		//检测是否需要将当前摄像机加入列表
		private bool Check(string camera_string)
		{
			if (camera_string == CameraMotion.CurrentStr)
			{
				CameraMotion.NeedChange = false;
				if (CameraMotion.CurrentCamera == null)
				{
					CameraMotion.CurrentCamera = GameObject.Find(MotionPlatformData.mainCameraStr).camera;
					CameraMotion.CameraString.Add(camera_string);
					CameraMotion.CameraList.Add(CameraMotion.CurrentCamera);
					if (CameraMotion.CurrentCamera.orthographic)
					{
						CameraMotion.CameraType.Add(false);  //正交
						CameraMotion.CurrentType = false;
					}
					else
					{
						CameraMotion.CameraType.Add(true);  //透视
						CameraMotion.CurrentType = true;
					}
				}
				return true;
			}
			int strIndex = CameraMotion.CameraString.IndexOf(camera_string);
			if (strIndex == -1)
			{  //不存在
				Camera tempCamera;
				try
				{
					tempCamera = GameObject.Find(camera_string).camera;
				}
				catch
				{
					return false;
				}
				CameraMotion.CameraString.Add(camera_string);
				CameraMotion.CameraList.Add(tempCamera);
				if (tempCamera.orthographic)
				{
					CameraMotion.CameraType.Add(false);  //正交
					CameraMotion.CurrentType = false;
				}
				else
				{
					CameraMotion.CameraType.Add(true);  //透视
					CameraMotion.CurrentType = true;
				}

				CameraMotion.FormerCamera = CameraMotion.CurrentCamera;
				CameraMotion.CurrentCamera = tempCamera;
				CameraMotion.CurrentStr = camera_string;
			}
			else
			{  //存在

				CameraMotion.FormerCamera = CameraMotion.CurrentCamera;
				CameraMotion.CurrentCamera = CameraMotion.CameraList[strIndex];
				CameraMotion.CurrentStr = CameraMotion.CameraString[strIndex];
				CameraMotion.CurrentType = CameraMotion.CameraType[strIndex];
			}

			CameraMotion.NeedChange = true;
			return true;
		}


		//Json文件加载
		private void JsonLoad(string path_name, CRSpline crSpline, ref bool is_right, bool is_back)
		{
			string filePath = MotionPlatformData.taskRootPath + MotionPlatformData.taskName + "/PathControl.json";
			if (File.Exists(filePath))
			{
				JsonOperator jsonOp = new JsonOperator();
				DataTable jsonTable = jsonOp.JsonReader(filePath, path_name);
				if (jsonTable == null)
				{
					Debug.LogError(path_name + ", 该路径名称不存在！");
					return;
				}
				if (is_back)
				{
					GameObject loadEmpty = new GameObject();
					loadEmpty.name = "JsonLoad_empty";
					GameObject loadFather = new GameObject();
					loadFather.name = "JsonLoad_Father";
					GameObject loadSon = new GameObject();
					loadSon.name = "JsonLoad_Son";
					loadSon.transform.parent = loadFather.transform;
					Vector3 endEulerAngles = Motion_Compute.Vector3Conversion((string)jsonTable.Rows[jsonTable.Rows.Count - 1][1].ToString(), ref is_right);
					loadSon.transform.localEulerAngles = endEulerAngles;
					loadSon.transform.parent = null;
					loadFather.transform.parent = loadSon.transform;
					loadSon.transform.eulerAngles = CameraMotion.CurrentCamera.transform.eulerAngles;
					loadFather.transform.parent = null;
					loadFather.transform.position = CameraMotion.CurrentCamera.transform.position;
					loadEmpty.transform.parent = loadFather.transform;
					for (int i = 0; i < jsonTable.Rows.Count; i++)
					{
						loadEmpty.transform.localPosition = Motion_Compute.Vector3Conversion((string)jsonTable.Rows[i][0].ToString(), ref is_right);
						loadEmpty.transform.localEulerAngles = Motion_Compute.Vector3Conversion((string)jsonTable.Rows[i][1].ToString(), ref is_right);
						crSpline.controlPoints.Add(loadEmpty.transform.position);
						crSpline.rotationList.Add(loadEmpty.transform.eulerAngles);
						crSpline.cameraViewList.Add(float.Parse((string)jsonTable.Rows[i][2].ToString()));
					}
					Vector3 posDiff = crSpline.controlPoints[crSpline.controlPoints.Count - 1] - crSpline.controlPoints[0];
					for (int i = 0; i < crSpline.controlPoints.Count; i++)
					{
						crSpline.controlPoints[i] = crSpline.controlPoints[i] - posDiff;
					}
					crSpline.controlPoints.Reverse();
					crSpline.rotationList.Reverse();
					crSpline.cameraViewList.Reverse();
					GameObject.DestroyImmediate(loadEmpty);
					GameObject.DestroyImmediate(loadFather);
					GameObject.DestroyImmediate(loadSon);
				}
				else
				{
					GameObject loadEmpty = new GameObject();
					loadEmpty.name = "JsonLoad_empty";
					loadEmpty.transform.parent = CameraMotion.CurrentCamera.transform;
					for (int i = 0; i < jsonTable.Rows.Count; i++)
					{
						loadEmpty.transform.localPosition = Motion_Compute.Vector3Conversion((string)jsonTable.Rows[i][0].ToString(), ref is_right);
						loadEmpty.transform.localEulerAngles = Motion_Compute.Vector3Conversion((string)jsonTable.Rows[i][1].ToString(), ref is_right);
						crSpline.controlPoints.Add(loadEmpty.transform.position);
						crSpline.rotationList.Add(loadEmpty.transform.eulerAngles);
						crSpline.cameraViewList.Add(float.Parse((string)jsonTable.Rows[i][2].ToString()));
					}
					GameObject.DestroyImmediate(loadEmpty);
				}
			}
			else
			{
				Debug.LogError(filePath + ", 该文件不存在！");
			}
		}

		private void AngleOptimization(List<Vector3> rotation_list)
		{
			for (int i = 1; i < rotation_list.Count; i++)
			{
				Vector3 tempVec = rotation_list[i];
				tempVec.x = AngleClerp(rotation_list[i - 1].x, rotation_list[i].x, 1f);
				tempVec.y = AngleClerp(rotation_list[i - 1].y, rotation_list[i].y, 1f);
				tempVec.z = AngleClerp(rotation_list[i - 1].z, rotation_list[i].z, 1f);
				rotation_list[i] = tempVec;
			}
		}

		//角度修改使其符合实际（类似-90和270这种情况处理）
		private float AngleClerp(float start, float end, float value)
		{
			float min = 0.0f;
			float max = 360.0f;
			float half = Mathf.Abs((max - min) / 2.0f);
			float retval = 0.0f;
			float diff = 0.0f;
			if ((end - start) < -half)
			{
				diff = ((max - start) + end) * value;
				retval = start + diff;
			}
			else if ((end - start) > half)
			{
				diff = -((max - end) + start) * value;
				retval = start + diff;
			}
			else retval = start + (end - start) * value;
			return retval;
		}
	}

}